//
//  CommuViewController.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/6.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  交流区

import UIKit
import WebKit


class CommuViewController : WKBaseViewController,WKBaseDelegate {
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.wkDelegate = self
        
        //背景色--白色
        view.backgroundColor = UIColor.white

        
        //取出本地token，进行拼接
//        if (localSave.object(forKey: userToken) != nil) {
        
            self.navigationItem.title = "交流区"
            if commuArray.count > 1 {
                let urlStr = commuArray.lastObject
                
                webView.load(URLRequest.init(url: URL.init(string: urlStr as! String)!))

            } else {
                DispatchQueue.main.async {
                    let urlStr = jlqURL + "?devtype=1&token=" + self.token
                    
                    
                    
                    //判断是否包含域名
                    if urlStr.contains(comStrURL) {
                        let url = URL.init(string: urlStr)

                        
                        TestNet.shared.getNetStatus(comfun: { (result) in
                            if result.rawValue == "worked" {
                                
                                DispatchQueue.main.async {
                                    
                                    self.urlRequestCache = NSURLRequest(url: url!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10)
                                    self.webView.load(self.urlRequestCache as URLRequest)
                                    
                                }
                                
                            } else {
                                
                                DispatchQueue.main.async {
                                    
                                    print("本地")
                                    print(self.webView.bounds)
                                    
                                    self.urlRequestCache = NSURLRequest.init(url: url!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 0)
                                    self.webView.load(self.urlRequestCache as URLRequest)
                                    
                                }
                                
                            }
                            
                        })
                    } else {
                        CustomAlertView.shared.alertWithTitle(strTitle: "请下拉刷新")

                    }
            }

    
        }
    }

    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        print("dddddd")
        return webView
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        url = (navigationAction.request.url?.absoluteString)!
        
        textView.text = "\((#file as NSString).lastPathComponent):(\(#line))" + url
        
        if navigationAction.navigationType == WKNavigationType.linkActivated || url == "http://yungou.ie1e.com/app/message_list.aspx" {
            
            print("\((#file as NSString).lastPathComponent):(\(#line))" + url)
            
            print("navigationType",url)

            if url.contains("?") {
                url = url + ("&devtype=1&token=") + (token)
            } else {
                url = url + ("?devtype=1&token=") + (token)
            }
            
            commuArray.add(url)
            
            //错误做法，拿数组最后一个元素跳转了 。。。。。。
            //            aaa(str: commuArray.lastObject as! String)
            aaa(str: url)
            
            decisionHandler(.cancel)

        } else {
            
            decisionHandler(.allow)
        
        }
        
    }
    
        //若不属于linkActived，则检查是否符合other类型
//        if navigationAction.navigationType == WKNavigationType.other {
//
//            if url == "http://yungou.ie1e.com/app/message_list.aspx" {
//
//                if url.contains("?") {
//                    url = url + ("&devtype=1&token=") + (token)
//                } else {
//                    url = url + ("?devtype=1&token=") + (token)
//                }
//                
//                aaa(str: url)
//                commuArray.add(url)
//   
//                print("other",url)
//            } else {
//                return
//            }
//
//        }
//    }
    
    //url---
    func aaa(str : String) -> Void {
        
        let vvv = CommuViewReplace()
        vvv.url = str
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vvv, animated: true)
        self.hidesBottomBarWhenPushed = false
        
    }
}

// MARK:- 实现代理方法
extension CommuViewController {
    func server() {
        //        aaa(str: fwqArray.lastObject as! String)
        
        print("啊ddd啊")
        self.navigationController?.popToRootViewController(animated: true)
        
        webView.load(URLRequest.init(url: URL.init(string: jlqURL)!))

    }

}

